ParametrizeCamera_LoadAndSave.py

Import/Export Camera Attribute File(s)

It shows how to import/export camera feature tree files. The major steps include exporting camera attributes to a file (such as FeatureFile.ini) by calling MV_CC_FeatureSave(), and importing camera feature tree from a file (such as FeatureFile.ini) by calling MV_CC_FeatureLoad().

1 # -- coding: utf-8 --
2 
3 import sys
4 import os
5 import platform
6 from ctypes import *
7 
8 # Compatible with different operating systems to load DDL
9 currentsystem = platform.system()
10 if currentsystem == 'Windows':
11  sys.path.append(os.path.join(os.getenv('MVCAM_COMMON_RUNENV'), "Samples", "Python", "MvImport"))
12 else:
13  sys.path.append(os.path.join("..", "..", "MvImport"))
14 from MvCameraControl_class import *
15 
16 # Compatible with input processing of Python 2.X and 3.X
17 if sys.version_info[0] < 3:
18  # Python 2.x
19  input_func = raw_input
20 else:
21  # Python 3.x
22  input_func = input
23 
24 # Decoding Characters
25 def decoding_char(ctypes_char_array):
26  """
27  Safely decode a string from a ctypes character array.
28  Compatible with Python 2.x and 3.x, as well as 32-bit and 64-bit environments.
29  """
30  byte_str = memoryview(ctypes_char_array).tobytes()
31 
32  # Truncate at the first null character
33  null_index = byte_str.find(b'\x00')
34  if null_index != -1:
35  byte_str = byte_str[:null_index]
36 
37  # Attempt to decode using multiple encodings
38  for encoding in ['gbk', 'utf-8', 'latin-1']:
39  try:
40  return byte_str.decode(encoding)
41  except UnicodeDecodeError:
42  continue
43 
44  # If all encodings fail, use a replacement strategy
45  return byte_str.decode('latin-1', errors='replace')
46 
47 
48 
49 if __name__ == "__main__":
50 
51  try:
52  # Initialize SDK resources
53  MvCamera.MV_CC_Initialize()
54 
55  SDKVersion = MvCamera.MV_CC_GetSDKVersion()
56  print ("SDKVersion[0x%x]" % SDKVersion)
57 
58  deviceList = MV_CC_DEVICE_INFO_LIST()
59  tlayerType = MV_GIGE_DEVICE | MV_USB_DEVICE
60 
61  # Enumerate devices
62  ret = MvCamera.MV_CC_EnumDevices(tlayerType, deviceList)
63  if ret != 0:
64  print ("enum devices fail! ret[0x%x]" % ret)
65  sys.exit()
66 
67  if deviceList.nDeviceNum == 0:
68  print ("find no device!")
69  sys.exit()
70 
71  print ("find %d devices!" % deviceList.nDeviceNum)
72 
73  for i in range(0, deviceList.nDeviceNum):
74  mvcc_dev_info = cast(deviceList.pDeviceInfo[i], POINTER(MV_CC_DEVICE_INFO)).contents
75  if mvcc_dev_info.nTLayerType == MV_GIGE_DEVICE :
76  print ("\ngige device: [%d]" % i)
77  strModeName = decoding_char(mvcc_dev_info.SpecialInfo.stGigEInfo.chModelName)
78  print ("device model name: %s" % strModeName)
79  strSerialNumber = decoding_char(mvcc_dev_info.SpecialInfo.stGigEInfo.chSerialNumber)
80  print("device serial number: %s" % strSerialNumber)
81  nip1 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24)
82  nip2 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16)
83  nip3 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8)
84  nip4 = (mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff)
85  print ("current ip: %d.%d.%d.%d\n" % (nip1, nip2, nip3, nip4))
86  elif mvcc_dev_info.nTLayerType == MV_USB_DEVICE:
87  print ("\nu3v device: [%d]" % i)
88  strModeName = decoding_char(mvcc_dev_info.SpecialInfo.stUsb3VInfo.chModelName)
89  print ("device model name: %s" % strModeName)
90 
91  strSerialNumber = decoding_char(mvcc_dev_info.SpecialInfo.stUsb3VInfo.chSerialNumber)
92  print ("device serial number: %s" % strSerialNumber)
93 
94  nConnectionNum = input_func("please input the number of the device to connect:")
95 
96  if int(nConnectionNum) >= deviceList.nDeviceNum:
97  print ("intput error!")
98  sys.exit()
99 
100  # Create the camera instance
101  cam = MvCamera()
102 
103  # Select a device, and create a handle
104  stDeviceList = cast(deviceList.pDeviceInfo[int(nConnectionNum)], POINTER(MV_CC_DEVICE_INFO)).contents
105 
106  ret = cam.MV_CC_CreateHandle(stDeviceList)
107  if ret != 0:
108  raise Exception ("create handle fail! ret[0x%x]" % ret)
109 
110  # Turn on the device
111  ret = cam.MV_CC_OpenDevice(MV_ACCESS_Exclusive, 0)
112  if ret != 0:
113  raise Exception ("open device fail! ret[0x%x]" % ret)
114 
115  print ("start export the camera properties to the file")
116  print ("wait......")
117 
118  # Export the camera features to the local file
119  ret = cam.MV_CC_FeatureSave("FeatureFile.mfs")
120  if MV_OK != ret:
121  raise Exception ("save feature fail! ret [0x%x]" % ret)
122  print ("finish export the camera properties to the file")
123 
124  print ("start import the camera properties from the file")
125  print ("wait......")
126 
127  # Import the camera features from the local file
128  ret = cam.MV_CC_FeatureLoad("FeatureFile.mfs")
129  if MV_OK != ret:
130  raise Exception ("load feature fail! ret [0x%x]" % ret)
131  print ("finish import the camera properties from the file")
132 
133  # Turn off the device
134  ret = cam.MV_CC_CloseDevice()
135  if ret != 0:
136  raise Exception ("close deivce fail! ret[0x%x]" % ret)
137 
138  # Destroy the handle
139  cam.MV_CC_DestroyHandle()
140 
141  except Exception as e:
142  print(e)
143  cam.MV_CC_CloseDevice()
144  cam.MV_CC_DestroyHandle()
145  finally:
146  # Release SDK resources
147  MvCamera.MV_CC_Finalize()